package network.step1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleServer1 {
	
	public static void main(String[] args) throws IOException{
		ServerSocket serverSocket = new ServerSocket(5000);
		//1.client 연결 대기
		Socket socket = null;
		BufferedReader br = null;
		
		while(true){
		try{
			//1. client 연결 대기
			socket = serverSocket.accept();
			//위에서 예외가 발생하면 예외처리를 여기서 잡아줌으로써 while문을 빠져나오지 못하게 한다.
		//IO작업
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String str = br.readLine();//1줄 input
			while(str != null){
				System.out.println(str);
				str = br.readLine();
			}
		
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(br != null) br.close();
			if(socket != null) socket.close();
		}
			
		}
		
	}
	
	
}
